package jdbc3;

import com.alibaba.druid.pool.DruidDataSourceFactory;

import javax.sql.DataSource;
import java.io.FileInputStream;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.Properties;

public class BruidDemo {
    public static void main(String[] args) throws Exception {
//        创建一个Properties对象
        Properties properties = new Properties();
//        创建一个FileInputStream对象，用于读取配置文件
        FileInputStream fileInputStream = new FileInputStream("src/jdbc3/jdbc.properties");
//        使用Properties对象加载配置文件
        properties.load(fileInputStream);
//        使用德鲁伊数据库连接池技术，创建一个dataSource对象
        DataSource dataSource = DruidDataSourceFactory.createDataSource(properties);
//        从 DataSource 中获取数据库连接
        Connection connection = dataSource.getConnection();
        System.out.println("connection = " + connection);

        String sql = "select * from students";
        PreparedStatement preparedStatement = connection.prepareStatement(sql);
        ResultSet resultSet = preparedStatement.executeQuery();

        ArrayList<Student> students = new ArrayList<>();
        while(resultSet.next()){
            Student student = new Student();
            student.setSno(resultSet.getString("sno"));
            student.setSname(resultSet.getString("sname"));
            student.setSsex(resultSet.getString("ssex"));
            student.setSbirthday(resultSet.getDate("sbirthday"));
            student.setClassname(resultSet.getString("classname"));
            students.add(student);
        }
        System.out.println("students = " + students);
    }
}
